
<template>
	<view class="s-back">
        <!-- <u-button @click="tologin" type="custom">重新登录</u-button> -->
	</view>
</template>

<script>
let s
	export default {
		name:"subscribe",
    components:{},
		data() {
			return {
			
            }
		},
		async onLoad() {
            s = this
      //       let isTokening = localStorage.getItem('isTokening')
			// let delayTime = isTokening?1000:0
			// localStorage.setItem('isTokening',0)
      // setTimeout(async()=>{
        
            if(uni.getStorageSync('token')) {
              s.afterLogin()
              return
            }
            if (window.location.href.indexOf('code') != -1) {
              // 其实用 localStorage 合适一些
              // localStorage.setItem('isTokening',1)
              localStorage.setItem('isTokening',0)
                let inviteId = uni.getStorageSync('inviteId')||''

                uni.setStorageSync('inviteId','')
                
                var code = window.location.href.match(/code=(\S*)&/)[1]
              
                var r = await s.$u.api.userWechatLogin({
                    code: code,
                    parent:inviteId
                 
                })
                if (r) {
                  uni.setStorageSync('token',r.data.token)
                  await s.getUserId()
                  s.getPageData()
                  s.getRemindData()
                  // localStorage.setItem('isTokening',1)
                  s.afterLogin()
                }
                
               
                } else {
                s.getCode()
                }

              // },delayTime)
		},
		mounted() {
			
		},
		
		computed:{
			
		},
		methods: {
      afterLogin() {
        const { redirect } = this.$mp.query
        if (redirect) {
          const url = decodeURIComponent(redirect)
          if (!url.includes('/pages/index/index')) {
            location.replace(url)
            return
          }
        }
        uni.switchTab({
          url:'/pages/index/index'
        })
      },
      async getUserId(){
				let r = await this.$u.api.userInfo()
				if (r) {
					localStorage.setItem('userId',r.data.id)
				}
			},
      async getPageData(){
				const r = await this.$u.api.configTitle()
				if(r){
					uni.setStorageSync('pageTitle',r.data.title)
				}
			},
			async getRemindData(){
	// 			"zdz_remind": "查出",
    // "welcome": "欢迎"
				const r = await this.$u.api.configRemind()
				if(r){
					uni.setStorageSync('welcome',r.data.welcome)
				}
			},
			async getCode() {
                let appid = ''
               const r = await s.$u.api.configWechat()
               if(r){
                appid = r.data.appid
               }
               console.log(window.location.href,'ssss')
              //  localStorage.setItem('isTokening',1)
      const redirectURL = encodeURIComponent(window.location.href)
      const base = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='
      let state = 'STATE'
      
      // let scope = 'snsapi_userinfo'
      let scope = 'snsapi_base'
      window.location.href =
        base +
        appid +
        '&redirect_uri=' +
        redirectURL +
        '&response_type=code&connect_redirect=1&scope=' +
        scope +
        '&state=' +
        state +
        '#wechat_redirect'
    },
		}
	}
</script>

<style lang="scss" scoped>

</style>
